package org.s170.dao;

import org.s170.entity.User;
import org.s170.utils.Dbutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    //查找所有用户
    public List<User> getAllUser(){
        List<User> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement psmt = null ;
        ResultSet rs = null ;
        User user =null;
        try {
            String sql = "select * from useInfo";
            conn =Dbutil.getConnection();
            psmt=conn.prepareStatement(sql);
            rs=psmt.executeQuery();
            while (rs.next()){
                user = new User();
                user.setUserid(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setSex(rs.getString(4));
                user.setEmail(rs.getString(5));
                user.setHeadImg(rs.getString(6));
                user.setRegisterTime(rs.getTimestamp(7));

                list.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbutil.closeAll(conn,psmt,rs);
        }
         return  list;
    }

    //根据账号密码查找用户
    public User findUser(String email , String password){
        Connection conn=null;
        PreparedStatement psmt=null;
        ResultSet rs=null;
        User user = null;
        try {
            String sql="select * from useInfo\n" +
                    "where email=? and pass=?";
            conn=Dbutil.getConnection();
            psmt=conn.prepareStatement(sql);
            psmt.setString(1,email);
            psmt.setString(2,password);
            rs=psmt.executeQuery();
            while (rs.next()){
                user = new User();
                user.setUserid(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setSex(rs.getString(4));
                user.setEmail(rs.getString(5));
                user.setHeadImg(rs.getString(6));
                user.setRegisterTime(rs.getTimestamp(7));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbutil.closeAll(conn,psmt,rs);
        }
        return user;
    }

    //根据用户名称来查询
    public User getIDFindUser(String username){
        Connection conn=null;
        PreparedStatement psmt=null;
        ResultSet rs=null;
        User user = null;
        try {
            String sql="select * from useInfo\n" +
                    "where username=?";
            conn=Dbutil.getConnection();
            psmt=conn.prepareStatement(sql);
            psmt.setString(1,username);
            rs=psmt.executeQuery();
            while (rs.next()){
                user = new User();
                user.setUserid(rs.getInt(1));
                user.setUsername(rs.getString(2));
                //user.setPassword(rs.getString(3));
                user.setSex(rs.getString(4));
                user.setEmail(rs.getString(5));
                user.setHeadImg(rs.getString(6));
                user.setRegisterTime(rs.getTimestamp(7));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbutil.closeAll(conn,psmt,rs);
        }
        return user;
    }

    public static void main(String[] args) {
        UserDao userDao=new UserDao();
        User user =userDao.findUser("121@qq.com","123456");
        System.out.println(user);
    }
}
